package models

import (
	"database/sql"

	_ "github.com/go-sql-driver/mysql"
)

func init() {
	//init
}

//url:/api/v1/fjsbs?fj_id=' + fjid
func Get_fjsbs(fjid int) (fjsbs []Sut_fjsb) {
	var (
		tmp_fjsb Sut_fjsb
		sqlstr   string
		stmt     *sql.Stmt
		rows     *sql.Rows
		err      error
	)
	sqlstr = "select fjsb_id,lyfj_id, sb_id , name , xinghao , guige ,changjia, remark,tag, sort from fjsb_list where lyfj_id=?" //调用视图
	stmt, err = db.Prepare(sqlstr)
	checkErr(err)
	rows, err = stmt.Query(fjid)
	checkErr(err)
	for rows.Next() {
		err = rows.Scan(&tmp_fjsb.Fjsb_id, &tmp_fjsb.Lyfj_id, &tmp_fjsb.Sb_id, &tmp_fjsb.Name, &tmp_fjsb.Xinghao, &tmp_fjsb.Guige, &tmp_fjsb.Changjia, &tmp_fjsb.Tag, &tmp_fjsb.Remark, &tmp_fjsb.Sort)
		checkErr(err)
		fjsbs = append(fjsbs, tmp_fjsb)
	}
	return fjsbs
}

//url:/api/v1/fjsbs_pzxx?fjsb_id=' + fjsb_id
func Get_fjsbs_pzxx(fjsb_id int) (fjsbs_pzxx []Sut_sb_pzxx) {
	var (
		tmp    Sut_sb_pzxx
		sqlstr string
		stmt   *sql.Stmt
		rows   *sql.Rows
		err    error
	)
	sqlstr = "select id,pzid,pzname,pzly,remark,tag,sort from fjsb_pzxx where fjsb_id=?" //调用视图
	stmt, err = db.Prepare(sqlstr)
	checkErr(err)
	rows, err = stmt.Query(fjsb_id)
	checkErr(err)
	for rows.Next() {
		err = rows.Scan(&tmp.Pz_id, &tmp.Pz_id, &tmp.Pz_name, &tmp.Pzly, &tmp.Remark, &tmp.Tag, &tmp.Sort)
		checkErr(err)
		fjsbs_pzxx = append(fjsbs_pzxx, tmp)
	}
	return fjsbs_pzxx
}

//添加房间设备
func Fjsb_add(fj_id int, sb_id int) (err error) {
	var (
		sqlstr string
		//result sql.Result
		//stmt *sql.Stmt
	)
	sqlstr = "insert t1_xm_fjsb set lyfj_id=?,sb_id=?" //调用视图
	//log.Println("func Fjsb_add(，sqlstr:", sqlstr)
	_, err = db.Exec(sqlstr, fj_id, sb_id)
	//log.Println("func Fjsb_add(，err:", err)
	//checkErr(err)
	//_, err = stmt.Exec(fj_id, sb_id)
	//log.Println("添加的行数：", result.RowsAffected(), "\t 添加的行的ID：", result.LastInsertId())
	return err
}
